package client.comm.baoding.adapter.main

import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.R
import client.comm.baoding.adapter.JxGood2Adapter
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.TradeIndex
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.SearchActivity
import client.comm.baoding.ui.TabOne2Activity
import client.comm.baoding.widget.HorizontalPageLayoutManager
import client.comm.baoding.widget.PagingScrollHelper
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.comm_ui.draw.SpaceGridItemDecoration
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import client.comm.commlib.getScreenWidth
import client.comm.commlib.utils.SharedUtil
import java.util.ArrayList

class GylHeaderAdapter(val manager: RequestManager, val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    var goodsIndex: CateGoods? = null

    companion object {

        val VIEW_TYPE = -0x09;
    }

    val jyCateAdapter: GylCateGoodAdapter by lazy {
        GylCateGoodAdapter(context, manager)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_gylheader,
                parent,
                false
            )
        ).apply {
            val itemBinding = this.binding as LayoutGylheaderBinding
            itemBinding.event = this@GylHeaderAdapter
            itemBinding.recyclerViewCate.layoutManager = GridLayoutManager(context, 3)
            itemBinding.recyclerViewCate.addItemDecoration(
                SpaceGridItemDecoration(
                    TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, 10.0f, context.resources.displayMetrics
                    ).toInt()
                )
            )
            itemBinding.recyclerViewCate.adapter = jyCateAdapter

        }
    }

    fun goSearch() {
        context.startActivity(Intent(context, SearchActivity::class.java).apply {
            putExtra("isAixin", true)
        })
    }

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {

        goodsIndex?.let {

            it.category_list?.let {
                jyCateAdapter.submitList(it)
            }
        }

    }

    var changeListType: ((String) -> Unit)? = null

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
        ) {
            lp.isFullSpan = true
        }
    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }

    override fun getStateViewType(loadState: LoadState): Int {
        return VIEW_TYPE
    }
}